import pygame
import sys
from game_spirit import *
import random


class MainGame:
    """游戏主体"""

    def __init__(self):
        pygame.init()
        pygame.display.set_caption("Flappy Bird")
        self.screen = pygame.display.set_mode(SIZE)
        self.clock = pygame.time.Clock()
        self.__creat_sprites()
        self.game_start = False
        self.game_over = False
        # 新水管出现的频率
        pygame.time.set_timer(CREAT_PIPE_EVENT, 1500)

    def __creat_sprites(self):
        # 创建背景精灵
        bg = BackGround()
        self.bg_group = pygame.sprite.Group(bg)
        # 创建陆地精灵
        land1 = Land()
        land2 = Land(True)
        self.land_group = pygame.sprite.Group(land1, land2)
        # 创建玩家控制的小鸟
        self.player = Bird()
        # self.player_group = pygame.sprite.Group(self.player)
        # 创建开始按钮
        self.play_button = Button('image/button_play.png', text='play')
        self.play_button.rect.center = (width / 2, 350)
        self.play_button.click_connect(self.__play)
        # 创建水管的精灵组
        self.pipe_group = pygame.sprite.Group()

    def __event_handler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    # print('鼠标左键按下')
                    self.player.is_down = False
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    self.player.is_down = True
            if event.type == CREAT_PIPE_EVENT:
                pipe_down = Pipe(True)
                pipe_up = Pipe()
                pipe_pos = random.randint(150, 300)
                pipe_down.rect.bottom = pipe_pos - 50
                pipe_up.rect.top = pipe_pos + 50
                self.pipe_group.add(pipe_down, pipe_up)

    def __check_collide(self):
        # 玩家小鸟落地
        if pygame.sprite.spritecollide(self.player, self.land_group, False):
            self.game_over = True
        elif pygame.sprite.spritecollide(self.player, self.pipe_group, False):
            self.game_over = True

    def __update_sprites(self):
        # 绘制背景图片
        self.bg_group.draw(self.screen)
        # 绘制水管
        self.pipe_group.update()
        self.pipe_group.draw(self.screen)
        # 更新和绘制地面
        self.land_group.update()
        self.land_group.draw(self.screen)
        # 更新和绘制玩家
        self.player.update()
        self.screen.blit(self.player.image, self.player.rect)

    def start_game(self):
        while True:
            if self.game_start:
                if self.game_over:
                    self.__game_over()
                else:
                    self.__event_handler()
                    self.__check_collide()
                    self.__update_sprites()
            else:
                self.__menu()
            pygame.display.update()
            self.clock.tick(FPS)

    def __menu(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            self.play_button.get_click(event)
        # 绘制背景图片
        self.bg_group.draw(self.screen)
        # 更新和绘制地面
        self.land_group.update()
        self.land_group.draw(self.screen)
        # 绘制菜单标题
        self.screen.blit(title_image, title_rect)
        # 绘制教程
        self.screen.blit(tutorial_image, tutorial_rect)
        # 更新和绘制玩家
        self.player.wave()
        self.screen.blit(self.player.image, self.player.rect)
        # 绘制开始游戏按钮
        self.play_button.draw(self.screen)

    def __play(self):
        self.player.reset()
        self.pipe_group.empty()
        self.game_start = True
        self.game_over = False

    def __game_over(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            self.play_button.get_click(event)

        # 绘制背景图片
        self.bg_group.draw(self.screen)
        # 绘制水管
        self.pipe_group.draw(self.screen)
        # 绘制地面
        self.land_group.draw(self.screen)
        # 绘制game over文字
        self.screen.blit(over_image, over_rect)
        # 绘制开始游戏按钮
        self.play_button.draw(self.screen)
        # 绘制玩家
        self.screen.blit(self.player.image, self.player.rect)


if __name__ == '__main__':
    game = MainGame()
    game.start_game()
